package com.bsoft.gol.hcb.dao.prescriptionCirculation;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import pcn.prescribe.OdsOrderToPrescription;
@SsdevDao(serviceId = "odsOrderToPrescriptionDAOService")
public abstract class OdsOrderToPrescriptionDAO extends HibernateSupportDelegateDAO<OdsOrderToPrescription> {
    public OdsOrderToPrescriptionDAO() {
        this.setKeyField("orderDetailId");
    }


    @DAOMethod(sql = "from OdsOrderToPrescription where orderId =:orderId")
    public abstract OdsOrderToPrescription getOrderId(@DAOParam("orderId")String orderId);

    @RpcService
    @DAOMethod(sql = "from OdsOrderToPrescription where orderId =:orderId")
    public abstract List<OdsOrderToPrescription> findOrderId(@DAOParam("orderId")String orderId);

    @RpcService
    @DAOMethod(sql = "select b.orderId from OdsOrderToPrescription a,OdsPrescriptionOrder b where b.orderId = a.orderId and a.feeCode in:feeCodeList and b.payStatus = '0'")
    public abstract List<String> findOrderIdListByfeeCodeList(@DAOParam("feeCodeList")List<String> feeCodeList);

    public List<Map> findOrderIds(final List<String> orderIds){
        HibernateStatelessResultAction<List<Map>> action = new AbstractHibernateStatelessResultAction<List<Map>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuilder sql = new StringBuilder();
                sql.append("select DISTINCT ootp.order_id as orderId,ootp.consult_type as  businessType,eod.express_code as expressCode," +
                        "eod.express_name as expressName, eod.express_order_id as expressOrderId  from ods_order_to_prescription ootp  ");
                sql.append(" left join express_order eo on eo.order_id = ootp.order_id\n");
                sql.append(" left join express_order_detail eod on eod.express_id = eo.express_id\n");
                sql.append(" where ootp.order_id in(:orderIds)");

                Query query = ss.createSQLQuery(sql.toString());
                query.setParameterList("orderIds",orderIds);

                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = query.list();
                setResult(dbList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @DAOMethod(sql = "from OdsOrderToPrescription where orderId =:orderId")
    public abstract List<OdsOrderToPrescription> findOrderToPrescriptionBy(@DAOParam("orderId")String orderId);

    @DAOMethod(sql = "select orderId from OdsOrderToPrescription where feeCode =:feeCode and consultId=:consultId")
    public abstract List<String> findOrderIdByFeeCode(@DAOParam("feeCode")String feeCode,@DAOParam("consultId")String consultId);

    @RpcService
    public OdsOrderToPrescription saveOdsOrderToPrescription(OdsOrderToPrescription odsOrderToPrescription){
        return this.save(odsOrderToPrescription);
    }

    @RpcService
    public void updateOdsOrderToPrescription(OdsOrderToPrescription odsOrderToPrescription){
        this.update(odsOrderToPrescription);
    }
}